package com.example.demo.contorller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.example.demo.Bean.ScoreBean;
import com.example.demo.Bean.Student;
import com.example.demo.Bean.Result;
import com.example.demo.Utils.ExcelUtil;
import com.example.demo.handle.ExcelHandle;
import com.example.demo.service.StudentService;
import org.apache.logging.log4j.message.ReusableMessage;
import org.apache.xmlbeans.impl.jam.mutable.MAnnotatedElement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import sun.security.ec.ECDHKeyAgreement;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
public class StudentController{

    @Autowired
    StudentService studentService;

    @RequestMapping("/getAllStudent")
    @CrossOrigin
    public Result getAllStudent(String courseId){
        try {
            return studentService.getAllStudent(courseId);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500,e.toString());
        }
    }

    @RequestMapping("/getStudentDetail")
    @CrossOrigin
    public Result getStudentDetail(String student_uid){
        try {
            return studentService.getStudentDetail(student_uid);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500,e.toString());
        }
    }

    @RequestMapping("/modifyStudentPassword")
    @CrossOrigin
    public Result modifyStudentPassword(String studentUid,String password){
        try {
            return studentService.modifyStudentPassword(studentUid, password);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500,e.toString());
        }
    }

    @RequestMapping("/modifyStudentDetail")
    @CrossOrigin
    public Result modifyStudentDetail(String studentUid,String studentName,String classees,String email){
        try {
            return studentService.modifyStudentDetail(studentUid, studentName, classees, email);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500,e.toString());
        }
    }

    @RequestMapping("/addStudent")
    @CrossOrigin
    public Result addStudent(String studentUid,String password,String classes,String name,String courseId){
        try {
            return studentService.addStudent(studentUid, password, classes, name, courseId);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500,e.toString());
        }
    }

    //导入Excel
    @RequestMapping(path="/importexcel.do",method = RequestMethod.POST)
    @CrossOrigin
    public Result importexcel(MultipartFile file,String courseId){
        try {
            EasyExcel.read(file.getInputStream(), Student.class, new ExcelHandle(studentService, courseId)).sheet().doRead();
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500,e.toString());
        }
        return new Result(200,"");
    }

    //导出为Excel
    @RequestMapping("/downloadexcel.do")
    @CrossOrigin
    public Result getExcel(HttpServletResponse response,String courseId) throws IllegalAccessException, IOException, InstantiationException {
        try {
            List<Student> list = (List<Student>) studentService.getAll(courseId).getObj();
            ExcelUtil.download(response, Student.class, list);
            return new Result(200, "");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500,e.toString());
        }
    }

    @RequestMapping("/searchStudent")
    @CrossOrigin
    public Result searchStudent(String name,String courseId,Integer pageNum,Integer pageSize){
        try {
            return studentService.searchStudent(name, courseId, pageNum, pageSize);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500,e.toString());
        }
    }

    @RequestMapping("/deleteStudent")
    @CrossOrigin
    public Result deleteStudent(String student_uid,String courseId){
        try {
            return studentService.deleteStudent(student_uid, courseId);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500,e.toString());
        }
    }

    @RequestMapping("/getStudentPage")
    @CrossOrigin
    public Result getStudentPage(Integer pageNum,Integer pageSize,String courseId){
        try {
            return studentService.getStudentPage(pageNum, pageSize, courseId);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500,e.toString());
        }
    }

    @RequestMapping("/getTotalStudentNum")
    @CrossOrigin
    public Result getTotalStudentNum(String courseId){
        try {
            return studentService.getTotalStudentNum(courseId);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500,e.toString());
        }
    }

    @RequestMapping("/getAll")
    public Result getAll(String courseId){
        try {
            return studentService.getAll(courseId);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500,e.toString());
        }
    }

    @RequestMapping(path = "/createGroup", method = RequestMethod.POST)
    @CrossOrigin
    public Result createGroup(@RequestBody Map<String, Object> map){
        try {
            String param = JSON.toJSONString(map.get("groupName"));
            String groupName = JSON.parseObject(param, String.class);
            param = JSON.toJSONString(map.get("ids"));
            List<String> ids = JSON.parseObject(param, ArrayList.class);
            param = JSON.toJSONString(map.get("courseId"));
            String courseId = JSON.parseObject(param, String.class);
            return studentService.createGroup(ids, groupName, courseId);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500,e.toString());
        }
    }

    @RequestMapping("/modifyGroupName")
    @CrossOrigin
    public Result modifyGroupName(Integer groupId, String name){
        try {
            return studentService.modifyGroupName(groupId, name);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500,e.toString());
        }
    }

    @RequestMapping("/getGroup")
    @CrossOrigin
    public Result getGroup(String courseId){
        try {
            return studentService.getGroup(courseId);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500,e.toString());
        }
    }

    @RequestMapping("/getGroupMember")
    @CrossOrigin
    public Result getGroupMember(Integer groupId){
        try {
            return studentService.getGroupMember(groupId);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500,e.toString());
        }
    }

        @RequestMapping("/deleteGroup")
    @CrossOrigin
    public Result deleteGroup(Integer groupId){
        try {
            return studentService.deleteGroup(groupId);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500,e.toString());
        }
    }

    @RequestMapping(path = "/deleteGroupMember", method = RequestMethod.POST)
    @CrossOrigin
    public Result updateGroupMember(@RequestBody Map<String, Object> map){
        try {
            String param = JSON.toJSONString(map.get("ids"));
            List<String> ids = JSON.parseObject(param, ArrayList.class);
            param = JSON.toJSONString(map.get("groupId"));
            Integer groupId = JSON.parseObject(param, Integer.class);
            return studentService.deleteGroupMember(ids, groupId);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500,e.toString());
        }
    }

    @RequestMapping(path = "/createGroupMember", method = RequestMethod.POST)
    @CrossOrigin
    public Result createGroupMember(@RequestBody Map<String, Object> map){
        try {
            String param = JSON.toJSONString(map.get("ids"));
            List<String> ids = JSON.parseObject(param, ArrayList.class);
            param = JSON.toJSONString(map.get("groupId"));
            Integer groupId = JSON.parseObject(param, Integer.class);
            return studentService.createGroupMember(ids, groupId);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500,e.toString());
        }
    }

    @RequestMapping(path = "/totalScore")
    @CrossOrigin
    public Result totalScore(@RequestBody Map<String,Object> map){
        try{
            String param = JSON.toJSONString(map.get("courseId"));
            String courseId = JSON.parseObject(param, String.class);
            param = JSON.toJSONString(map.get("scoreBeans"));
            List<ScoreBean> scoreBeans = JSON.parseArray(param, ScoreBean.class);
            return studentService.totalScore(courseId, scoreBeans);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500,e.toString());
        }
    }

}
